﻿package org.zengrong.utils{
	import flash.display.*;
	import flash.geom.*;

	/**
	 *  切割图片及位图透明化的函数，用于处理人物行走、魔法招式等图片。
	 *  getBitRects(source,4,3)  把source按4行3列切成12张bitmapData
	 *  getBitPixel(source,400,300)  把source切成若干张宽400高300的图片，原图不满的地方会补白
	 *  transparent(source)    取source左上角像素颜色,与此颜色的差异小于阈值的像素全部透明
	 * 
	 */	
	public class BitmapTool {
		public static function getBitRects(source:DisplayObject, col:int, row:int,
									valid:* = null, trans:Boolean = true):Array {
			var w:Number = source.width / col;
			var h:Number = source.height / row;
			valid = (valid == null) ? col * row : valid;
			
			var target:Array = incise(source, w, h, col, row, valid, trans);
			return target;
		}

		public static function getBitPixel(source:DisplayObject, w:Number, h:Number, valid:* = null, trans:Boolean = false):Array {
			var col:int = Math.ceil(source.width / w);
			var row:int = Math.ceil(source.height / h);
			valid = (valid === null) ? col * row : valid; //总数
			
			var target:Array = incise(source, w, h, col, row, valid, trans);
			return target;
		}

		/**
		 * 位图透明化
		 * @param source 源图
		 * @param arg1          颜色值或坐标
		 */		
		public static function transparent(source:*, arg1:* = null, arg2:* = null):BitmapData {
			var threshold:uint;
			var src:BitmapData = source is Bitmap ? source.bitmapData : source;
			if (arg1 == null){
				threshold = src.getPixel32(0, 0); 
			} else {
				if (arg2 == null){
					threshold = arg1;
				} else {
					threshold = src.getPixel32(arg1, arg2);
				}
			}
			//trace(threshold.toString(16))
			
			var rect:Rectangle = new Rectangle(0, 0, src.width, src.height);
			var point:Point = new Point(0, 0);
			var result:BitmapData = new BitmapData(src.width, src.height, true);
			result.copyPixels(src, rect, point);
			
			/*
			黑色透明 0x0  		    黑色不透明0xff000000
			白色透明0x00FFFFFF  白色不透明0xffffffff
			
			 如果 (pixelValue & mask) operation (threshold & mask)，则将像素设置为 color；
			否则，如果 copySource == true，则将像素设置为 sourceBitmap 中的对应像素值。
			*/
			result.threshold(src, rect, point, "==", threshold, 0x0, 0xFFFFFFFF, true);
			return result;
		}

		/**
		 *  按据draw分割显示对象
		 *  矩阵方法的使用
		 *  draw(source, matrix, null, null, rect, true)
		 */		
		private static function incise(source:DisplayObject, w:Number, h:Number, col:int,
									   row:int, valid:int, trans:Boolean):Array {
			var target:Array = [];
			var matrix:Matrix = new Matrix(); //矩阵
			var rect:Rectangle = new Rectangle(0, 0, w, h);
			
			out:for (var j:int = 0; j < row; j++){
				for (var i:int = 0; i < col; i++){
					if (valid <= 0)
						break out;
					var bmp:BitmapData = new BitmapData(w, h, trans, 0);
					matrix.tx = -i * w;
					matrix.ty = -j * h;
					bmp.draw(source, matrix, null, null, rect, true);
					target.push(bmp);
					valid--;
				}
			}
			return target;
		}
		/**
		 *  按copyPixels方法分割显示对象
		 *  copyPixels(src,rect,point)
		 */		
		public static function getBitmaps(bitmap:BitmapData,column:uint,row:uint):Vector.<BitmapData>{
			var arr:Vector.<BitmapData> = new Vector.<BitmapData>();
			var w:uint = bitmap.width / column; //列
			var h:uint = bitmap.height / row;   //行
			var rect:Rectangle = new Rectangle();
			var point:Point = new Point();
			
			for (var px:int = 0; px < column; px++){
				for (var py:int = 0; py < row; py++){
					rect.x = px * w;
					rect.y = py * h;
					rect.width = w;
					rect.height= h;
					
					var temp:BitmapData = new BitmapData(w, h);
					temp.copyPixels(bitmap, rect, point, null, null, true);
					arr.push(temp);
				}
			}
			return arr;
		}
	}
}